package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.DateCondition;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    OrderSettingDao orderSettingDao;
    @Override
    public List<OrderSetting> findAll() {
        return orderSettingDao.findAll();
    }

    @Override
    public void addOrderSettingList(ArrayList<OrderSetting> orderSettingList) {
        if (orderSettingList!=null&&orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                OrderSetting orderSettingDB=orderSettingDao.findByDate(orderSetting.getOrderDate());
                if (orderSettingDB!=null){
                    orderSettingDao.update(orderSetting);
                }else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<OrderSetting> findByDateCondition(DateCondition dateCondition) {
        return orderSettingDao.findByDateCondition(dateCondition);
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        OrderSetting  orderSettingDB= orderSettingDao.findByDate(orderSetting.getOrderDate());
        if (orderSettingDB!=null){
            orderSettingDao.update(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }

    @Override
    public void delOrderSetting() throws Exception {
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        orderSettingDao.delOrderSetting(today);
    }
}
